package com.sport.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.sport.mapper.SysUserMapper;
import com.sport.service.Impl.SysUserServiceImpl;
import com.sport.utils.SecurityUtil;
import com.sun.corba.se.impl.orbutil.concurrent.SyncUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@AllArgsConstructor
@NoArgsConstructor
@Data
@ApiModel(value = "用户实体")
public class SysUser implements UserDetails {

    @Autowired
    SysUserMapper sysUserMapper;

    private Integer id;

    @ApiModelProperty(value = "用户名")

    private String username;

    @ApiModelProperty(value = "前端要展示的用户名")
    private String name;

    @ApiModelProperty(value = "密码")
    private String password;

    @ApiModelProperty(value = "邮箱")
    private String email;

    @ApiModelProperty(value = "昵称")
    private String nickName;

    @ApiModelProperty(value = "状态")
    private boolean state;

    @ApiModelProperty(value = "微信获取用户头像")
    private String avatar;

    @ApiModelProperty(value = "性别")
    private Integer sex;

    @ApiModelProperty(value = "微信小程序用户唯一标识")
    private String openId;

    @ApiModelProperty("微信获取用户地址")
    private String address;

    @ApiModelProperty(value = "电话号码")
    private String phoneNumber;

    private boolean admin;
    private  boolean status;

    @ApiModelProperty(value = "角色对应的菜单列表")
    private List<SysMenu> menus;
    @ApiModelProperty(value = "用户对应的权限列表")
      private List<SysPermission> permissions;
    private List<SysRole> roles;

    @Override
    @JsonIgnore
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> list=new ArrayList<>();
//        list.add(new SimpleGrantedAuthority("ROLE_admin"));
        if(roles!=null &&roles .size()>0){
        roles.forEach(item->
            list.add(new SimpleGrantedAuthority("ROLE_"+item.getCode())));

        }
        if(permissions!=null && permissions.size()>0){
            permissions.forEach(item->list.add(new SimpleGrantedAuthority(item.getCode())));
        }
        return list;

    }

    @Override
    @JsonIgnore
    public boolean isAccountNonExpired() {
        return false;
    }

    @Override
    @JsonIgnore
    public boolean isAccountNonLocked() {
        return false;
    }

    @Override
    @JsonIgnore
    public boolean isCredentialsNonExpired() {
        return false;
    }

    @Override
    @JsonIgnore
    public boolean isEnabled() {
        return status;
    }

    public boolean isAdmin() {
      return admin;
    }
}
